package com.ww.learn.coroutine.actors

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.channels.actor

/**
 * 生产者、消费者两个调度器，通过Channel通信
 */
fun main() = runBlocking {
    val consumer: SendChannel<Int> = actor<Int>(Dispatchers.Default) {
        for (data in channel) {
            com.ww.learn.coroutine.delay(150)
            println("消费：$data")
        }
    }

    launch(Dispatchers.IO) {
        for (i in 1..5) {
            com.ww.learn.coroutine.delay(100)
            consumer.send(i)
            println("生产：$i")
        }
        consumer.close()
    }.join()
}